package com.googlecode.vmock;

import com.googlecode.vmock.utils.Validate;

/**
 * A call result that is expected to execute an {@link Answer} stub and return the value returned by it. It can also throw exceptions if the given
 * stub also throws them.
 * 
 * @author marcin.jagus
 */
class AnswerCallResult extends AbstractExpectedCallResult {

    /**
     * Implementation of a funcional interface which produces the expected result.
     */
    private Answer<?> answer;

    /**
     * Creates new result with a given answer and recorded call.
     * 
     * @param answer Implementation of a functional interface which produces the expected result
     * @param recordedCall a call recorded in {@link MockStage#RECORDING} stage
     */
    public AnswerCallResult(Answer<?> answer, RecordedCall recordedCall) {
        super(recordedCall);
        Validate.notNull(answer, "answer == null");

        this.answer = answer;
    }

    @Override
    public Object getResult(Object[] params) {
        incrementTimesInvoked();
        return answer.answer(params);
    }

}
